package cn.bluethink.eguan.core.service;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.bluethink.eguan.core.entity.EgCheckEntity;
import cn.bluethink.eguan.core.mapper.EgCheckMapper;
import cn.bluethink.eguan.model.core.EgCheck;
import cn.bluethink.eguan.model.core.EgCheckStatus;
import cn.bluethink.eguan.model.core.EgOType;
import cn.bluethink.eguan.model.core.EgTavern;
import cn.bluethink.eguan.model.core.EgUser;
import cn.bluethink.eguan.utils.IdMakerUtils;

/**
 * 审核
 * 
 * @author 苏州中科蓝迪公司所有(c)2016-2021
 * @version 0.3.0
 * @brief 驿馆+产品
 * @note 修订历史： 1.fanbl于2019/2/19设计并构建初始版本v1.0
 */
@Service
public class EgCheckService {

	private IdMakerUtils idMakerUtil = new IdMakerUtils();

	@Autowired
	private EgCheckMapper checkMapper;

	@Autowired
	private EgUserService userService;

	@Autowired
	private EgTavernService tavernService;

	@Autowired
	private EgTaskService taskService;

	/**
	 * 审核
	 * 
	 * @param egCheck
	 * @param uid
	 * @return
	 * @throws Exception
	 */
	@Transactional(transactionManager = "eguanTransactionManager", rollbackFor = Exception.class)
	public EgCheck check(EgCheck check, Long uid) throws Exception {
		// 创建审核记录id
		Long id = idMakerUtil.nextId();
		check.setId(id);
		EgUser user = userService.getUser(uid, false);
		check.setChecker(user);
		check.setcTime(new Date());
		// 1、转实体类
		EgCheckEntity entity = new EgCheckEntity(check);
		// 2、新增审核记录
		if (checkMapper.insert(entity) == 0) throw new Exception("审核失败！");
		// 3、获取审核状态
		Integer status = check.getStatus();
		// 4、审核未通过，删除审核对象
		if (status == EgCheckStatus.STATUS_NOPASS) {
			entity = deleteAuditedObj(entity);
			if (entity == null) throw new Exception("审核失败！");
			return check;
		}
		// 5、审核通过，修改审核状态
		if (status == EgCheckStatus.STATUS_PASSED) {
			entity = updateAuditedObj(entity);
			if(entity == null) throw new Exception("审核失败！");
			return check;
		}
		return check;
	}

	/**'
	 * 删除被审核对象
	 * @param entity
	 * @return
	 * @throws Exception
	 */
	private EgCheckEntity deleteAuditedObj(EgCheckEntity entity) throws Exception {
		// 1、如果是驿馆，删除驿馆
		if (entity.getOtype() == EgOType.TAVERN) {
			Long del = tavernService.del(entity.getOid());
			if (del == null) return null;
			return entity;
		}
		// 2、如果是 工作/悬赏 任务，逻辑删除通用任务，使用定时器删除工作/悬赏任务
		if (entity.getOtype() == EgOType.REWARD_TASK || entity.getOtype() == EgOType.OKR_TASK) {
			Long remove = taskService.remove(entity.getOid(), entity.getOtype());
			if (remove == 0L) return null;
			return entity;
		}
		return null;
	}

	/**
	 * 更新被审核对象审核状态
	 * @param entity
	 * @return
	 * @throws Exception
	 */
	private EgCheckEntity updateAuditedObj(EgCheckEntity entity) throws Exception {
		// 1、修改驿馆审核状态
		if (entity.getOtype() == EgOType.TAVERN) {
			EgTavern egTavern = new EgTavern();
			egTavern.setCheckStatus(EgCheckStatus.STATUS_PASSED);
			egTavern.setId(entity.getOid());
			EgTavern update = tavernService.update(egTavern);
			if (update == null) return null;
			return entity;
		}
		// 2、修改悬赏/工作 任务审核状态
		if (entity.getOtype() == EgOType.REWARD_TASK || entity.getOtype() == EgOType.OKR_TASK) {
			int result = taskService.updateCheckStatus(EgCheckStatus.STATUS_PASSED, entity.getOid());
			if (result == 0) return null;
			return entity;
		}
		return null;

	}

}
